package com.app.view.component.dialogs 
{
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	
	import com.greensock.TweenLite;
	import com.greensock.TimelineLite;
	import com.greensock.easing.*;
	
	import org.component.Button;
	
	import com.peachcastle.core.Dialog;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.component.room.seat.PaijiuType;
	
	import com.app.view.component.dialogs.pkResultView.UserInfo;
	import com.app.view.component.dialogs.pkResultView.UserPaijiuGroup;
	import com.app.view.component.dialogs.pkResultView.PaijiuTypeList;
	import com.app.view.component.dialogs.pkResultView.TieInfo;
	import com.app.view.component.dialogs.pkResultView.Tab;
	import com.app.view.component.dialogs.pkResultView.PkResultData;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class PkResultView extends Dialog
	{
		private var _main:MovieClip = null;
		
		private var _tab:Tab = null;
		
		private var _pkData:PkResultData = null;
		
		private var _firstPoint:Point = null;
		private var _firstWidth:Number = 0;
		private var _firstHeight:Number = 0;
		
		private var _moveSmallPoint:Point = null;
		private var _moveNormalPoint:Point = null;
		
		private var _dialogManagerMediator:DialogManagerMediator = null;
		
		private var _banker:UserInfo = null;
		private var _playr:UserInfo = null;
		
		private var _bankerFirstGroup:UserPaijiuGroup = null;
		private var _bankerSecondGroup:UserPaijiuGroup = null;
		private var _playrFirstGroup:UserPaijiuGroup = null;
		private var _playrSecondGroup:UserPaijiuGroup = null;
		
		private var _tieInfo:TieInfo = null;
		
		private var _firstPkArrow:MovieClip = null;
		private var _secondPkArrow:MovieClip = null;
		
		private var _flagArray:Array = ['banker_mc_win', 'banker_mc_loss', 'banker_mc_giveup',
										'playr_mc_win', 'playr_mc_loss', 'playr_mc_giveup'];
		
		private var _strObj:Object = {'bankerFirst': '莊1', 'bankerSecond': '莊2', 'playrFirst': '闲1', 'playrSecond': '闲2'};
		
		private var _paijiuRank:Array = null;
		
		private var _btn_close:Button = null;
		
		private var _list:PaijiuTypeList = null;
		
		private var _timeline:TimelineLite = null;
		
		private var _animationLock:Boolean = false;
		
		public function PkResultView(dialogUrlFunc:Function, dialogManagerMediator:DialogManagerMediator) 
		{
			_dialogManagerMediator = dialogManagerMediator;
			super("PkResultView", false, dialogUrlFunc,
					_dialogManagerMediator.stageSize["width"], _dialogManagerMediator.stageSize["height"]);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			super.move(centerX + 65, 150);
			
			_main = assest["main"];
			
			_tab = new Tab(assest, this);
			
			_pkData = new PkResultData();
			
			_firstPoint = new Point(assest.x, assest.y);

			_banker = new UserInfo(_main["banker"]);
			_playr = new UserInfo(_main["playr"]);
			
			_bankerFirstGroup = new UserPaijiuGroup(_main["mc_banker_first"], 1);
			_bankerSecondGroup = new UserPaijiuGroup(_main["mc_banker_second"], 2);
			_playrFirstGroup = new UserPaijiuGroup(_main["mc_playr_first"], 3);
			_playrSecondGroup = new UserPaijiuGroup(_main["mc_playr_second"], 4);
			
			_tieInfo = new TieInfo(_main["mc_tie_info"]);
			
			_firstPkArrow = _main["mc_first_pk_arrow"];
			_firstPkArrow["txt_label"].text = "";
			_firstPkArrow.alpha = 0;
			_secondPkArrow = _main["mc_second_pk_arrow"];
			_secondPkArrow["txt_label"].text = "";
			_secondPkArrow.alpha = 0;
			
			_btn_close = new Button(assest["btn_close"]);
			
			_list = new PaijiuTypeList(_main["list"]);
		}
		
		override protected function configureEventListeners():void 
		{
			super.configureEventListeners();
			
			_btn_close.addEventListener(MouseEvent.CLICK, onClose);
		}
		
		private function addMainClickEventListener():void
		{
			_main.doubleClickEnabled = true;
			_main.mouseChildren = false;
			_main.buttonMode = true;
			_main.addEventListener(MouseEvent.CLICK, onAssestClick);
		}
	
		private function onClose(event:MouseEvent = null):void
		{
			_tieInfo.hideAssest();
			
			_btn_close.removeEventListener(MouseEvent.CLICK, onClose);
			_main.removeEventListener(MouseEvent.CLICK, onAssestClick);
			
			super.hide();
		}
		
		private function onAssestClick(e:MouseEvent):void
		{
			_main.buttonMode = false;
			_tieInfo.hideAssest();
		}
		
		private function parseData():void
		{
			if (data['isStartPk']) 
			{
				_pkData.rest();
				_tab.hideAll();
			}
			
			if (!_pkData.isHasBankerPaijiu())
			{
				var bankerPaijiu:Array = data['bankerPais'].split(",");
				_pkData.setBanker([bankerPaijiu[0], bankerPaijiu[1]], [bankerPaijiu[2], bankerPaijiu[3]], data['banker']);
			}
			
			var playrPaijiu:Array = data['playrPais'].split(",");
			_pkData.setPlayr(data['playr'].seatid, [playrPaijiu[0], playrPaijiu[1]], [playrPaijiu[2], playrPaijiu[3]], 
									data['playr'], data['pkStatus'], data['banker_max'], data['playr_max'], data['pkType']);
		}
		
		override protected function showAction():void
		{
			configureEventListeners();
			setStage(_dialogManagerMediator.roomSize["width"], _dialogManagerMediator.roomSize["height"]);
			
			if (data.hasOwnProperty("isClick") && data['isClick'])
			{
				showResult(data['seatid']);
			}else
			{
				parseData();
				
				if (!data['isSelf'] || data['isBanker'])
				{
					if(!_animationLock) onClose();
					return;
				}

				showResult(data['playr'].seatid, true, true);
			}
		}
		
		public function showResult(seatid:int, isShowAnimation:Boolean = false, isAutoClose:Boolean = false):void
		{
			if (!_pkData.getPlayr(seatid)) return;
			
			_list.update();
			
			if (isShowAnimation)
			{
				showResultInAnimation(seatid, isAutoClose);
			}else
			{
				showResultAtOnce(seatid);
			}
		}
		
		public function showResultAtOnce(seatid:int):void
		{
			_tab.showOne(_pkData.getPlayr(seatid));
			
			_banker.show(_pkData.getBanker());
			_playr.show(_pkData.getPlayr(seatid));

			var pkResult:Array = _pkData.getPlayrResult(seatid);
			
			for each(var s:String in _flagArray)
			{
				MovieClip(_main[s]).visible = false;
			}
			
			if (pkResult[3]  == 1)
			{
				if (pkResult[0] >= 2)
				{
					MovieClip(_main[_flagArray[0]]).visible = true;
				}else
				{
					MovieClip(_main[_flagArray[3]]).visible = true;
				}
			}else
			{
				MovieClip(_main[_flagArray[2]]).visible = true;
			}
			
			var pkR:int = 0;
			var b:Array = [], p:Array = [];
			b = PaijiuType.judgeType(_pkData.getBankerPaijiuFirstGroup());
			_bankerFirstGroup.show(_pkData.getBankerPaijiuFirstGroup(), b[1]);
			
			p = PaijiuType.judgeType(_pkData.getPlayrPaijiuFirstGroup(seatid));
			_playrFirstGroup.show(_pkData.getPlayrPaijiuFirstGroup(seatid), p[1]);
			
			pkR = PaijiuType.pk(b, p);
			if (pkR == 1)
			{
				_firstPkArrow["txt_label"].text = "莊贏";
			}else if(pkR == 0)
			{
				_firstPkArrow["txt_label"].text = "和牌";
			}else
			{
				_firstPkArrow["txt_label"].text = "闲贏";
			}
			_firstPkArrow.alpha = 1;
			
			b = PaijiuType.judgeType(_pkData.getBankerPaijiuSecondGroup());
			_bankerSecondGroup.show(_pkData.getBankerPaijiuSecondGroup(), b[1]);
			
			p = PaijiuType.judgeType(_pkData.getPlayrPaijiuSecondGroup(seatid));
			_playrSecondGroup.show(_pkData.getPlayrPaijiuSecondGroup(seatid), p[1]);
			
			pkR = PaijiuType.pk(b, p);
			if (pkR == 1)
			{
				_secondPkArrow["txt_label"].text = "莊贏";
			}else if(pkR == 0)
			{
				_secondPkArrow["txt_label"].text = "和牌";
			}else
			{
				_secondPkArrow["txt_label"].text = "闲贏";
			}
			_secondPkArrow.alpha = 1;

			_tieInfo.hideAssest();
			if (pkResult[1] != 0 && pkResult[2] != 0)
			{
				addMainClickEventListener();
				_tieInfo.showAssest();
				_tieInfo.setData(_pkData.getBankerPaijiuFirstGroup().concat(_pkData.getBankerPaijiuSecondGroup()),
									_pkData.getPlayrPaijiuFirstGroup(seatid).concat(_pkData.getPlayrPaijiuSecondGroup(seatid)),
									pkResult[1],
									pkResult[2]
								);
			}	
		}
		
		public function showResultInAnimation(seatid:int, isAutoClose:Boolean):void
		{
			if (_animationLock) return;
			
			_animationLock = true;
			
			_tab.showOne(_pkData.getPlayr(seatid));
			
			_banker.show(_pkData.getBanker());
			_playr.show(_pkData.getPlayr(seatid));
			
			reset();
			
			_timeline = new TimelineLite();
			
			var pkResult:Array = _pkData.getPlayrResult(seatid);
			
			var pkR:int = 0;
			var b:Array = [], p:Array = [];
			b = PaijiuType.judgeType(_pkData.getBankerPaijiuFirstGroup());
			_timeline.append( TweenLite.delayedCall(1, _bankerFirstGroup.show, [_pkData.getBankerPaijiuFirstGroup(), b[1]]) );
			
			p = PaijiuType.judgeType(_pkData.getPlayrPaijiuFirstGroup(seatid));
			_timeline.append( TweenLite.delayedCall(1, _playrFirstGroup.show, [_pkData.getPlayrPaijiuFirstGroup(seatid), p[1]]) );
			
			pkR = PaijiuType.pk(b, p);
			if (pkR == 1)
			{
				_firstPkArrow["txt_label"].text = "莊贏";
			}else if(pkR == 0)
			{
				_firstPkArrow["txt_label"].text = "和牌";
			}else
			{
				_firstPkArrow["txt_label"].text = "闲贏";
			}
			_timeline.append( TweenLite.to(_firstPkArrow, 1, {alpha:1}) );
			
			b = PaijiuType.judgeType(_pkData.getBankerPaijiuSecondGroup());
			_timeline.append( TweenLite.delayedCall(1, _bankerSecondGroup.show, [_pkData.getBankerPaijiuSecondGroup(), b[1]]) );
			
			p = PaijiuType.judgeType(_pkData.getPlayrPaijiuSecondGroup(seatid));
			_timeline.append( TweenLite.delayedCall(1, _playrSecondGroup.show, [_pkData.getPlayrPaijiuSecondGroup(seatid), p[1]]) );
			
			pkR = PaijiuType.pk(b, p);
			if (pkR == 1)
			{
				_secondPkArrow["txt_label"].text = "莊贏";
			}else if(pkR == 0)
			{
				_secondPkArrow["txt_label"].text = "和牌";
			}else
			{
				_secondPkArrow["txt_label"].text = "闲贏";
			}
			_timeline.append( TweenLite.to(_secondPkArrow, 1, {alpha:1}) );

			if (pkResult[1] != 0 && pkResult[2] != 0)
			{
				addMainClickEventListener();
				_tieInfo.setData(_pkData.getBankerPaijiuFirstGroup().concat(_pkData.getBankerPaijiuSecondGroup()),
									_pkData.getPlayrPaijiuFirstGroup(seatid).concat(_pkData.getPlayrPaijiuSecondGroup(seatid)),
									pkResult[1],
									pkResult[2]
								);
				_timeline.append( TweenLite.delayedCall(1, _tieInfo.showAssest) );
			}	
			
			for each(var s:String in _flagArray)
			{
				MovieClip(_main[s]).visible = false;
				MovieClip(_main[s]).alpha = 0;
			}
			
			if (pkResult[3] == 1)
			{
				if (pkResult[0] >= 2)
				{
					MovieClip(_main[_flagArray[0]]).visible = true;
					_timeline.append( TweenLite.to(MovieClip(_main[_flagArray[0]]), 1, {alpha:1}) );
				}else
				{
					MovieClip(_main[_flagArray[3]]).visible = true;
					_timeline.append( TweenLite.to(MovieClip(_main[_flagArray[3]]), 1, {alpha:1}) );
				}
			}else
			{
				MovieClip(_main[_flagArray[2]]).visible = true;
				_timeline.append( TweenLite.to(MovieClip(_main[_flagArray[2]]), 1, {alpha:1}) );
			}
			
			if (isAutoClose) _timeline.append( TweenLite.delayedCall(1, onClose) );
			
			_timeline.append( TweenLite.delayedCall(2, unAnimationLock) );
		}
		
		private function unAnimationLock():void
		{
			_animationLock = false;
		}
		
		private function reset():void
		{
			assest.x = _firstPoint.x;
			assest.y = _firstPoint.y;
			
			_bankerFirstGroup.hideAssest();
			_playrFirstGroup.hideAssest();
			
			_bankerSecondGroup.hideAssest();
			_playrSecondGroup.hideAssest();
			
			_firstPkArrow.alpha = 0;
			_secondPkArrow.alpha = 0;
			
			_tieInfo.hideAssest();
		}
	}

}